package com.platform.service;

import com.platform.dao.ApiUserDeptMapper;
import com.platform.dao.SysDeptDao;
import com.platform.dao.SysUserDao;
import com.platform.dao.SysUserRoleDao;
import com.platform.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Service实现类
 *
 * @author lipengjun
 * @email 939961241@qq.com
 * @date 2019-02-22 12:28:09
 */
@Service("apiUserDeptService")
public class ApiUserDeptService{
    @Autowired
    private ApiUserDeptMapper apiUserDeptMapper;
    @Autowired
    private SysDeptDao sysDeptDao;
    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private SysUserRoleDao sysUserRoleDao;
    @Autowired
    private ApiChannelService channelService;


    public UserDeptVo applyOpenShop(Long shopUserId, String myShopName, Integer myShopType) {

        //创建商家
        SysDeptEntity rootDept = new SysDeptEntity();
        rootDept.setName(myShopName);
        rootDept.setRootType(myShopType);
        rootDept.setParentId(0L);
        rootDept.setIsCerted(1);
        rootDept.setIsActive(1);
        rootDept.setAppBgImageUrl("http://aiyitech-platform-wxmall.oss-cn-shenzhen.aliyuncs.com/upload/20190131/160236128c4bb2.png");
        sysDeptDao.save(rootDept);

        //创建管理员
        SysUserEntity admin = new SysUserEntity();
        admin.setUsername("admin"+rootDept.getDeptId());
        admin.setPassword("92925488b28ab12584ac8fcaa8a27a0f497b2c62940c8f4fbc8ef19ebc87c43e");  //密文，明文：888888
        admin.setStatus(1);
        admin.setCreateTime(new Date());
        admin.setDeptId(rootDept.getDeptId());
        admin.setIsSuperAdmin(0);
        sysUserDao.save(admin);

        //授权管理员默认角色
        Map<String, Object> map = new HashMap<>();
        map.put("userId", admin.getUserId());

        List<Long> roleIdList = new ArrayList<Long>();
        roleIdList.add(1L);
        map.put("roleIdList", roleIdList);

        sysUserRoleDao.save(map);

        //微信会员和管理员作关联
        UserDeptVo vo = new UserDeptVo();
        vo.setRegister_date(new Date());
        vo.setRoot_dept_id(rootDept.getDeptId());
        vo.setShop_user_id(shopUserId);
        vo.setSys_user_id(admin.getUserId());
        vo.setSys_user_name(admin.getUsername());
        vo.setRoot_dept_name(rootDept.getName());
        apiUserDeptMapper.save(vo);

        //新建渠道
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("root_dept_id", 9); //参照左岸进行复制
        param.put("offset", 0);
        param.put("limit", 5);
        param.put("sidx", "sort_order ");
        param.put("order", "asc ");
        List<ChannelVo> channels = channelService.queryList(param);
        if(channels != null && channels.size() > 0){
            for(ChannelVo channel : channels){
                ChannelVo cha = new ChannelVo();
                cha.setName(channel.getName());
                cha.setIcon_url(channel.getIcon_url());
                cha.setUrl(channel.getUrl());
                cha.setSort_order(channel.getSort_order());
                cha.setRoot_dept_id(rootDept.getDeptId());
                channelService.save(cha);
            }
        }

        return vo;
    }

    public int countByDeptName(String myShopName) {
        return sysDeptDao.countByDeptName(myShopName);
    }

    public List<UserDeptVo> queryOpenShop(Long userId) {
        return apiUserDeptMapper.queryOpenShop(userId);
    }

}
